package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.fc.entity.ProcessApply;
import cn.lg.soar.boot.fc.entity.vo.ProcessRecordVo;
import cn.lg.soar.boot.fc.service.ProcessApplyService;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.IPageQuery;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.ParameterUtil;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.database.util.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.fc.mapper.ProcessRecordMapper;
import cn.lg.soar.boot.fc.entity.ProcessRecord;
import cn.lg.soar.boot.fc.service.ProcessRecordService;

import java.util.ArrayList;
import java.util.List;

/**
 * 审批记录表
 * @author ZhuanZ（无密码）
 * @since 2025-01-14
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class ProcessRecordServiceImpl extends BaseServiceImpl<ProcessRecordMapper, ProcessRecord> implements ProcessRecordService {

    @Autowired
    private ProcessApplyService processApplyService;

    private SqueryWapper<ProcessRecord> processRecordSqueryWapper;

    @Override
    public HttpResult<PageResult<ProcessRecordVo>> getProcessRecordVo(PageQuery pageQuery, QueryParams params, Long applyid) {
        //将fields集合置为空
        params.setFields(new ArrayList<>());
        PageResult<ProcessRecordVo> resultVo = new PageResult<>();

        //流程应用名称
        String processName = processApplyService.getById(applyid).getProcessName();

        //先查询当前数据列表
        if (processRecordSqueryWapper == null) {
            processRecordSqueryWapper = new SqueryWapper<>(ProcessRecord.class);
        }

        QueryWrapper<ProcessRecord> processRecordWrappers = Wrappers.query();

        //查询where //只查询id=applyid
        List<String> processRecordF = new ArrayList<>();
        processRecordF.add("processApplyId-eq~"+applyid);

        if (pageQuery==null || pageQuery.getPageSize()<=0) {
            processRecordSqueryWapper.setWhere(processRecordF,processRecordWrappers);
            processRecordSqueryWapper.setSelect(params.getFields(),processRecordWrappers);
            //所有数据
            List<ProcessRecord> list = super.list(processRecordWrappers);
            //存储数据
            List<ProcessRecordVo> voList =new ArrayList<>();

            for (ProcessRecord processRecord : list) {
                ProcessRecordVo vo = new ProcessRecordVo();
                BeanUtils.copyProperties(processRecord, vo);
                vo.setProcessName(processName);
                voList.add(vo);
            }
            //塞入模型数据
            resultVo.setList(voList);
            return  HttpResult.data(resultVo);
        }
        resultVo.setPageNo(pageQuery.getPageNo());
        resultVo.setPageSize(pageQuery.getPageSize());
        resultVo.setQueryType(pageQuery.getQueryType());

        if (IPageQuery.QueryType.total.equals(pageQuery.getQueryType())) {
            processRecordSqueryWapper.setWhere(processRecordF,processRecordWrappers);
            resultVo.setTotal(super.count(processRecordWrappers));
        }
        IPage<ProcessRecord> page = PageUtils.toPage(pageQuery);
        processRecordSqueryWapper.setWhere(processRecordF,processRecordWrappers);
        processRecordSqueryWapper.setSelect(params.getFields(),processRecordWrappers);
        processRecordSqueryWapper.setOrderBy(params.getOrderBy(),processRecordWrappers);

        page = super.page(page,processRecordWrappers);
        resultVo.setTotal(page.getTotal());

        //存储模型数据List
        List<ProcessRecordVo> voList =new ArrayList<>();
        //分页所有数据
        List<ProcessRecord> records = page.getRecords();
        //复制数据
        for (ProcessRecord record : records) {
            ProcessRecordVo vo = new ProcessRecordVo();
            BeanUtils.copyProperties(record, vo);
            //塞入流程应用名称
            vo.setProcessName(processName);
            voList.add(vo);
        }
        //塞入模型数据
        resultVo.setList(voList);

        return HttpResult.data(resultVo);
    }

    @Override
    public HttpResult<ProcessRecordVo> getById(Long id) {
        ParameterUtil.notNull(id, "缺少数据id");
        //返回数据
        ProcessRecordVo vo = new ProcessRecordVo();
        //原表数据
        ProcessRecord processRecord = super.getById(id);
        //复制数据
        BeanUtils.copyProperties(processRecord, vo);

        //根据流程应用id获取流程应用名称
        ProcessApply processApply = processApplyService.getById(processRecord.getProcessApplyId());
        //返回的模型表中加入流程应用名称
        vo.setProcessName(processApply.getProcessName());

        return HttpResult.data(vo);
    }
}
